﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using PMS.SANY.Entity;
using PMS.SANY.IBusiness;
using PMS.SANY.Util;
using PMS.SANY.Models;

namespace PMS.SANY.Business
{
    public class MaterielSortingIssuedBusiness : BusRepository<MaterielSortingIssued>, IMaterielSortingIssuedBusiness, IScopedDependency
    {
        private IServiceProvider SvcProvider { get; set; }
        public MaterielSortingIssuedBusiness(GDbContext context, IServiceProvider svcProvider)
            : base(context)
        {
            this.SvcProvider = svcProvider;
        }

        public async Task<PageResult<MaterielSortingIssued>> GetPageResultAsync(PageInput<MaterielSortingIssuedQM> query)
        {
            var queryable = this.GetQueryable(true);
            var search = query.Search;
            queryable = queryable.Where(w => w.SortingConfigId == search.SortingConfigId);

            var result = await this.GetPageResultAsync(queryable, query);
            if (!search.Id.IsNullOrEmpty())
            {
                if (!result.Data.Any(w => w.Id == search.Id))
                {
                    var idOne = await this.GetQueryable(true).SingleOrDefaultAsync(w => w.Id == search.Id);
                    result.Data.Add(idOne);
                    result.Total += 1;
                }
            }
            return result;
        }
    }
}
